package com.meishu.sdkdemo.adactivity.banner;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meishu.sdk.core.AdSdk;
import com.meishu.sdk.core.ad.banner.BannerAdListener;
import com.meishu.sdk.core.ad.banner.BannerAdLoader;
import com.meishu.sdk.core.ad.banner.IBannerAd;
import com.meishu.sdk.core.loader.AdPlatformError;
import com.meishu.sdk.core.loader.InteractionListener;
import com.meishu.sdk.core.loader.ShareInfo;
import com.meishu.sdk.core.loader.ShareInteractionListener;
import com.meishu.sdkdemo.R;
import com.meishu.sdkdemo.adid.IdProviderFactory;

public class BannerAdActivity extends AppCompatActivity implements View.OnClickListener, BannerAdListener {
    private static final String TAG = "BannerADActivity";

    private BannerAdLoader bannerLoader;
    private ViewGroup bannerContainer;

    private boolean showCloseButton = true;
    private TextView showBannerAd;
    private IBannerAd bannerAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_ad);

        bannerContainer = findViewById(R.id.bannerContainer);
        showBannerAd = findViewById(R.id.showBannerAd);
        Button bannerAD = findViewById(R.id.loadBannerAd);
        bannerAD.setOnClickListener(this);
        showBannerAd.setOnClickListener(this);
        findViewById(R.id.loadBannerAdWithoutCloseBtn).setOnClickListener(this);

        ((EditText) findViewById(R.id.alternativeBannerAdPlaceID)).setText(IdProviderFactory.getDefaultProvider().banner());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loadBannerAd:
            case R.id.loadBannerAdWithoutCloseBtn:
                showCloseButton = v.getId() == R.id.loadBannerAd;

                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(),0);
                }

                String pid  = ((EditText) findViewById(R.id.alternativeBannerAdPlaceID)).getText().toString().trim();
                if (TextUtils.isEmpty(pid)) {
                    pid = IdProviderFactory.getDefaultProvider().banner();
                }

                bannerContainer.removeAllViews();
                bannerLoader = new BannerAdLoader(this, pid, this);
                bannerLoader.loadAd();
                break;

            case R.id.showBannerAd:

                if (bannerAd!=null){
                    // 不显示关闭按钮，仅限msad
                    bannerAd.setCloseButtonVisible(showCloseButton);
                    // 适应 container 的大小需要设置宽高，仅限msad
                    bannerAd.setWidthAndHeight(bannerContainer.getMeasuredWidth(), bannerContainer.getMeasuredHeight());
                    //bannerContainer.addView(bannerAd.getAdView());
                    bannerAd.setInteractionListener(new ShareInteractionListener() {

                        @Override
                        public void onShareButtonClicked(ShareInfo shareInfo) {
                            Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()) + " " + shareInfo);
                        }

                        @Override
                        public void onAdClicked() {
                            Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
                        }
                    });
                    bannerAd.showAd(bannerContainer);
                }
                break;
        }
    }

    /**
     * 注意：广告加载成功不要再做原有逻辑处理，应该转移到onAdReady中
     * 广告加载成功，此时广告不一定会渲染成功，这里不做任何处理，所有处理统一放到onAdReady中
     */
    @Override
    public void onAdLoaded(IBannerAd bannerAd) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
        Toast.makeText(BannerAdActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAdExposure() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdClosed() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdError() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdPlatformError(AdPlatformError e) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()) + " " + e);
    }

    /**
     * 广告已经准备完毕，此时可以进行广告的展示操作和相关逻辑处理
     */
    @Override
    public void onAdReady(IBannerAd iBannerAd) {
        this.bannerAd = iBannerAd;
    }

    /***
     * 渲染失败
     * @param msg
     * @param code
     */
    @Override
    public void onAdRenderFail(String msg, int code) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.bannerLoader != null) {
            this.bannerLoader.destroy();
        }
    }
}
